package org.hu.vhr.controller.Sys.basic;

import org.hu.vhr.model.Department;
import org.hu.vhr.model.RespBean;
import org.hu.vhr.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理
 * @author Sweeney
 */
@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @GetMapping("/")
    public List<Department> getAllDeps(){
        return departmentService.getAllDepsByParentId(-1);
    }

    @PostMapping("/")
    public RespBean addDep(@RequestBody Department dep){
        departmentService.addDep(dep);
        if(dep.getResult()==1){
        return RespBean.success("添加成功",dep);
        }
        return RespBean.error("添加失败");
    }

    @DeleteMapping("/{id}")
    public RespBean addDep(@PathVariable Integer id){
        Department dep = new Department();
        dep.setId(id);
        departmentService.delDep(dep);
        if(dep.getResult()==-2){
            return RespBean.error("该部门存在子部门，删除失败");
        }else if(dep.getResult()==-1){
            return RespBean.error("该部门存在员工，删除失败");
        }else if(dep.getResult()==1){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
